//
// Created by jaesonzhang on 2024/9/13.
//

#include "Context.h"

namespace skiacanvas {
Context::Context(uint32_t width, uint32_t height)
    : width(width), height(height) {
  SkImageInfo
      info =
      SkImageInfo::MakeS32(width, height, SkAlphaType::kPremul_SkAlphaType);
  // 分配目标Bitmap的像素内存
  skBitmap.allocPixels(info);
  new(&skCanvas)SkCanvas(skBitmap);
  paint.setColor(SK_ColorWHITE);
  SkRect rect;
  rect.set(SkIRect::MakeWH(width, height));
  skCanvas.drawRect(rect, paint);
}

void Context::fillText(const char *str, uint32_t x, uint32_t y) {
  textPaint.setColor(SK_ColorRED);
  SkFont skfont(SkTypeface::MakeDefault(), 50);
  skCanvas.drawString(SkString(str), x, y, skfont, textPaint);
}

void Context::fillRect(uint32_t startX,
                       uint32_t startY,
                       uint32_t endX,
                       uint32_t endY) {
//  SkRect rect;
//  rect.set(SkIRect::MakeLTRB(startX, startY, endX, endY));
//  skCanvas.drawRect(rect, paint);
//  SkRect rect;
//  rect.set(SkIRect::MakeWH(width, height));
//  skCanvas.drawRect(rect, paint);

  // 创建一个 SkRect 对象，指定矩形的坐标范围
  SkRect rect = SkRect::MakeLTRB(startX, startY, endX, endY);

  // 创建一个 SkPaint 对象，用于设置绘制属性
  SkPaint paint;
  paint.setColor(SK_ColorWHITE);
  paint.setStyle(SkPaint::kFill_Style); // 设置绘制样式为填充
  // 使用 SkCanvas 的 drawRect 方法绘制矩形
  skCanvas.drawRect(rect, paint);
}

} // end of namespace skiacanvas
